<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,button{
            padding:0;
            margin:0;
        }
        button{
            width: 150px;
            height: 40px;
            text-align:center;
            font:20px/40px microsoft yahei;
            margin:20px;
        }
        #box{
            width: 200px;
            height: 200px;
            background-color:lightblue;
            position:absolute;
            top:100px;
            left:0;
        }
    </style>
</head>
<body>
<button>向右下移动</button>
<div id="box"></div>
<script src="../MyJs.js"></script>
<script>
    var btn=document.getElementsByTagName("button")[0];
    var box=document.getElementById("box");
    btn.onclick=function(){
        doMove(box,3,500,"left",function(){
            doMove(box,3,600,"top",function(){
                alert("到达目的地！");
            });
        })

    }
    function doMove(element,step,end,atrr,endFn){
        step=parseInt(getStyle(element,atrr))>end?-step:step;
        clearInterval(element.timer);
        element.timer=setInterval(function(){
            var nowposition=parseInt(getStyle(element,atrr))+step;
            if(parseInt(getStyle(element,atrr))>=end&&step>0||parseInt(getStyle(element,atrr))<=end&&step<0){
                nowposition=end;
            }
            element.style[atrr]=nowposition+"px";
            if(parseInt(getStyle(element,atrr))===end){
                clearInterval(element.timer);
                endFn&&endFn();
            }

        },30)
    }
</script>
</body>
</html>